package com.bencordingley.remotecontrol;

import java.util.Comparator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MediaBrowserAdapter extends ArrayAdapter<MediaItem> implements Comparator<MediaItem> {

	private static LayoutInflater inflater;

	public MediaBrowserAdapter(Context context, int textViewResourceId, MediaItem[] objects) {
		super(context, textViewResourceId, objects);
		if (inflater == null) {
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		sort(this);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MediaItem mi = getItem(position);

		View container = inflater.inflate(R.layout.file_item, null);

		ImageView icon = (ImageView) container.findViewById(R.id.Image_icon);
		if (mi.isDirectory()) {
			icon.setImageResource(R.drawable.ic_list_directory);
		} else {
			icon.setImageResource(R.drawable.ic_list_file);
		}

		TextView mrl = (TextView) container.findViewById(R.id.Text_mrl);
		mrl.setText(mi.getLocation());

		return container;
	}

	@Override
	public int compare(MediaItem object1, MediaItem object2) {
		// TODO make 1 come before 10
		if (object1.isDirectory()) {
			if (object2.isDirectory()) {
				return object1.getLocation().compareToIgnoreCase(object2.getLocation());
			} else {
				return 1;
			}
		} else {
			if (object2.isDirectory()) {
				return 1;
			} else {
				return object1.getLocation().compareToIgnoreCase(object2.getLocation());
			}
		}
	}

}
